package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.MySharedPreferences;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 时间：2020/5/26 15:19
 * 功能：意见反馈
 */
public class FeedbackActivity extends BaseActivity {

    @BindView(R.id.submit_feedback)
    Button submitFeedback;
    @BindView(R.id.feedback_context)
    EditText feedbackContext;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);
    }

    @OnClick(R.id.submit_feedback)
    public void onViewClicked() {

        if (feedbackContext.getText().toString().equals("")){
            Toast("输入内容");
            return;
        }

        getPulesStatus("1",feedbackContext.getText().toString());

    }


    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:
                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        Toast(object.getString("msg"));
                        if (object.getInt("code") == 1){

                            feedbackContext.setText("");

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };

    /**
     * 检测脉象
     */
    public void getPulesStatus(String type, String content) {

        List<String> ls = new ArrayList<>();
        ls.add(content);
        ls.add(MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        ls.add("");
        ls.add(type);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "advise");
        params.put("data[content]", content);
        params.put("data[picture]", "");
        params.put("data[type]", type);
        params.put("data[member_id]", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/api", params, handler, 1, 404);

    }

}
